package com.ctgu.rabbitmq.rabbitmq.workQueue;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ ClassName WorkQueueConfig
 * @ Author Alex2
 * @ Date 2025/8/23 16:12
 **/
@Configuration
public class WorkQueueConfig {

    public static final String WORK_QUEUE = "work_queue";

    // 声明一个工作队列（持久化）
    @Bean(name = WORK_QUEUE)
    public Queue workQueue() {
        return QueueBuilder.durable(WORK_QUEUE).build();
    }

    @Bean
    public SimpleRabbitListenerContainerFactory workQueueListenerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);

        factory.setConcurrentConsumers(2);      // 两个线程同时消费
        factory.setMaxConcurrentConsumers(2);   // 最大线程数
        factory.setPrefetchCount(1);            // 每个线程一次只拿1条消息 → 公平分发
        factory.setAcknowledgeMode(AcknowledgeMode.AUTO); // 自动ACK
        return factory;
    }
}
